#!/bin/sh
#
# Start chrony

DAEMON="chronyd"
# /var/run/chrony is owned by the chrony user, this allows chrony to
# delete the PID file during shutdown, long after dropping privileges.
PIDFILE="/var/run/chrony/$DAEMON.pid"
CHRONY_ARGS=""

# shellcheck source=/dev/null
[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"

start() {
	printf "Starting %s: " "$DAEMON"
	# shellcheck disable=SC2086 # we need the word splitting
	start-stop-daemon --start --pidfile "$PIDFILE" \
		--exec "/usr/sbin/$DAEMON" \
		-- $CHRONY_ARGS
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

stop() {
	printf "Stopping %s: " "$DAEMON"
	start-stop-daemon --stop --pidfile "$PIDFILE" \
		--exec "/usr/sbin/$DAEMON"
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	while [ -f "$PIDFILE" ]; do
		sleep 0.1
	done
	return "$status"
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
esac

exit $?
